package xxgf

import (
	"fmt"
	"gitee.com/fenglei-l/ops-util/xxlog"
	"github.com/gogf/gf/v2/net/ghttp"
)

type Response struct {
	Code   string      `json:"code"`
	Result interface{} `json:"result"`
	Msg    interface{} `json:"message"`
}
type ResponsePage struct {
	Code   string      `json:"code"`
	Result interface{} `json:"result"`
	Total  int64       `json:"total"`
	Msg    interface{} `json:"message"`
}

const (
	ERROR   = "error"
	SUCCESS = "success"
)

func result(r *ghttp.Request, code string, data interface{}, msg interface{}) {
	r.Response.WriteJsonExit(Response{
		code,
		data,
		msg,
	})
	return
}
func resultPage(r *ghttp.Request, code string, data interface{}, total int64, msg interface{}) {
	r.Response.WriteJsonExit(ResponsePage{
		code,
		data,
		total,
		msg,
	})
	return
}

// FailWithData 错误
func FailWithData(r *ghttp.Request, msg string) {
	xxlog.ErrorUp366(fmt.Errorf("request:%s %s", r.RequestURI, msg))
	result(r, ERROR, map[string]interface{}{}, msg)
}

// FailWithDataInfoLog 错误不记录日志
func FailWithDataInfoLog(r *ghttp.Request, msg string) {
	xxlog.InfoUp366F("request:%s %s", r.RequestURI, msg)
	result(r, ERROR, map[string]interface{}{}, msg)
}

func OkWithData(r *ghttp.Request, content interface{}) {
	result(r, SUCCESS, content, content)
}
func ApiWriteJson(r *ghttp.Request, content interface{}) {
	r.Response.WriteJsonExit(content)
}
func ApiWrite(r *ghttp.Request, content ...interface{}) {
	r.Response.WriteExit(content)
}
func OkWithDataPage(r *ghttp.Request, content interface{}, total int64) {
	resultPage(r, SUCCESS, content, total, "success")
}
